<?php

namespace app\admin\controller;

use app\admin\logic\BranchLogic;
use app\admin\logic\ServerLogic;
use app\admin\logic\UserLogic;
use app\admin\logic\WorkLogic;
use app\admin\logic\CheckLogic;
use think\Request;

class Work extends Base
{
    private $workLogic;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->workLogic = new WorkLogic();
    }

    // 列表页
    public function index()
    {
        $works = $this->workLogic->query('');
        // p($works);
        $this->assign('list', $works);
        return $this->fetch();
    }


    // 营业额统计页
    public function indextotal()
    {
        $users = (new UserLogic())->query(' roles in (8, 16) ');
        $this->assign('list', $users);
        return $this->fetch();
    }

    //新增工单页面
    public function addFront($id = 0)
    {
        $work = null;
        if ($id > 0) {
            $work = $this->workLogic->findOne($id);
        }
        $this->assign('work', $work);
        return $this->fetch();
    }

    //选择车牌页面
    public function chooseCarNum()
    {
        return $this->fetch();
    }

    //保存数据
    public function doAdd($param = '')
    {
        $workArr = json_decode($param, true);
        $res = $this->workLogic->add($workArr);
        return $res;
    }

    // 维修技师添加工单，有工单数据，有服务项目数据
    public function doAddTech($param = '', $servers = '')
    {
        $res = $this->workLogic->addTech($param, $servers);
        return $res;
    }

    //删除数据
    public function del($id = 0)
    {
        $res = $this->workLogic->del($id);
        return $res;
    }


    // 前台人员添加工单页面
    public function addWork()
    {
        //接待人员数据
        $userLogic = new UserLogic();
        $reception = $userLogic->query(['roles' => 16, 'branch_id' => session('branch_id')]);
        $this->assign('reception', $reception);
        return $this->fetch();
    }

    // 前台人员打开工单页面
    public function indexWork()
    {
        return $this->fetch();
    }

    // 前台人员添加工单页面 单form表单
    public function addWorkSingleForm()
    {
        //接待人员数据
        $userLogic = new UserLogic();
        $reception = $userLogic->query(['roles' => 16, 'branch_id' => session('branch_id')]);
        $this->assign('reception', $reception);
        //日期
        $date = date('Y-m-d H:i:s', time());
        $this->assign('date', $date);
        // 工单号
        $workNum = generateWorkNum();
        $this->assign('worknum', $workNum);

        return $this->fetch();
    }

    // 施工中工单页面获取数据
    public function getWork($page = 1, $rows = 10, $q = '')
    {
        $data = $this->workLogic->queryJqGridNoPaied('', $page, $rows, $q);
        return $data;
    }

    //获取工单数据
    public function getWorkServer($page = 1, $rows = 10, $q = '')
    {
        $data = $this->workLogic->queryJqGrid('', $page, $rows, $q);
        return $data;
    }

    //获取工单数据
    public function getWorkTotal($page = 1, $rows = 10, $q = '')
    {
        $param = json_decode($q, true);
        $where = " paid_time is not null ";

        if (!empty($param["user_id"])) {
            $where .= " and (reception_id=$param[user_id] or repairman_id=$param[user_id]) ";
        }

        if (!empty($param["sdate"])) {
            $where .= " and paid_time>" . strtotime($param["sdate"]);
        }

        if (!empty($param["edate"])) {
            $where .= " and paid_time<" . (strtotime($param["edate"]) + 3600 * 24);
        }

        //echo $where;
        $data = $this->workLogic->queryJqGrid($where, $page, $rows, json_encode($param));
        return $data;
    }

    //获取工单数据
    public function getTotal($q = '')
    {
        $param = json_decode($q, true);
        $where = " paid_time is not null ";

        if (!empty($param["user_id"])) {
            $where .= " and (reception_id=$param[user_id] or repairman_id=$param[user_id]) ";
        }

        if (!empty($param["sdate"])) {
            $where .= " and paid_time>" . strtotime($param["sdate"]);
        }

        if (!empty($param["edate"])) {
            $where .= " and paid_time<" . (strtotime($param["edate"]) + 3600 * 24);
        }

        $data = $this->workLogic->queryTotal($where, json_encode($param));
        return $data;
    }

    // 待结账工单界面获取数据
    public function getWorkPaied($page = 1, $rows = 10, $q = '')
    {
        $data = $this->workLogic->queryJqGridPaied('', $page, $rows, $q);
        return $data;
    }

    // 待结账工单界面获取数据
    public function getPayedWorkList($page = 1, $rows = 10, $q = '')
    {
        $data = $this->workLogic->queryJqGridPayed('', $page, $rows, $q);
        return $data;
    }

    // 获取待提醒的客户信息
    public function getWorkNotifyClient($page = 1, $rows = 10, $q = '')
    {
        $data = $this->workLogic->queryJqGridNotify('', $page, $rows, $q);
        return $data;
    }


    // 根据车牌号选择最近一次该车保养信息中的客户信息
    public function getLinkedClient($car_num = '')
    {
        return $this->workLogic->getLinkedClient($car_num);
    }

    // 前台在拿到技师返回的单子后，继续填写工单
    public function modifyWork($id = 0)
    {
        // 已经填过的信息
        $work = $this->workLogic->findOne($id);
        // p($work);
        $this->assign('work', $work);
        $nextDate = date('Y-m-d', strtotime('+5 Month', strtotime($work['date'])));
        $this->assign('nextDate', $nextDate);
        //接待人员数据
        $userLogic = new UserLogic();
        $reception = $userLogic->query(['roles' => 16, 'branch_id' => session('branch_id')]);
        $this->assign('reception', $reception);
        //维修技师人员
        $userLogic = new UserLogic();
        $repairmen = $userLogic->query(['roles' => 8, 'branch_id' => session('branch_id')]);
        $this->assign('repairmen', $repairmen);
        //检查项目数据
        $checkLogic = new CheckLogic();
        $checks = $checkLogic->findDefaultCheck();
        $this->assign('check', $checks);
        //服务项目数据
        $serverLogic = new ServerLogic();
        $servers = $serverLogic->getServerItems($id);
        $this->assign('server', $servers['rows']);
        // p($servers);
        return $this->fetch();
    }

    public function doModifyWork($param = '', $server = '', $tbServer = '')
    {

        $res1 = $this->workLogic->addTech($server, $tbServer);
        $res2 = $this->workLogic->doModifyWork($param);

        return $res1 & $res2;
    }

    /*
     * 设置已提醒
     */
    public function doNotify($param = '')
    {
        return $this->workLogic->doModifyWork($param);
    }

    public function doPay($param = '')
    {
        return $this->workLogic->doPay($param);
    }


    public function doRecharge($param = '')
    {
        return $this->workLogic->doRecharge($param);
    }

    public function test()
    {
        $nextNum = generateWorkNum();
        p($nextNum);
    }

}


